# -*- coding:utf-8 -*-
# @Time : 2020/1/10 16:00
# @Author: liangkun
# @File : auto_test03
# @Des : 自动化app测试案例【以安卓为例】

# 1、安装 Appium 【https://github.com/appium/appium-desktop/releases】 找到exe文件
# 2、安装Andriodstudio工具
# 3、安装 appium-python-client 插件

from appium import webdriver
import time, traceback
from appium.webdriver.common.touch_action import TouchAction
# 配置参数
# 定义启动设备需要的参数
from appium.common import logger
from selenium.common.exceptions import NoSuchElementException

desired_caps = {}
# 设备系统
desired_caps['platformName'] = 'Android'
# 设备系统版本号
desired_caps['platformVersion'] = '6.0'
# 设备名称
desired_caps['deviceName'] = '乐1s'
# 要测试的应用的地址
desired_caps['app'] = r'D:\Personal\Desktop\pythontest\hsc_phone_V1.0_debug.apk'
# 应用的包名
desired_caps['appPackage'] = 'com.app.hscphone'
desired_caps['appActivity'] = 'com.app.hscphone.ui.activity.SplashActivity'
desired_caps["noReset"] = True
desired_caps["unicodeKeyboard"] = True
desired_caps["resetKeyboard"] = True
# 启动app
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)


# 模拟登录，点击进入主界面，下拉刷新
def test():
    try:
        print('已经登录了。。。。')
        driver.implicitly_wait(10)
        driver.find_element_by_id("com.app.hscphone:id/tv_search").get_attribute("text")
        driver.find_element_by_id('com.app.hscphone:id/ll_device').click()
        time.sleep(1)
        driver.find_element_by_id('com.app.hscphone:id/tv_dia_confirm').click()
        time.sleep(3)
        # 模拟用户滑动
        swipeDown(driver)
        # 点击人物[找不到resource_id]待做
    except NoSuchElementException:
        print('NoSuchElementException。。。。')
        print(traceback.format_exc())
        # 每次登录
        driver.find_element_by_id('com.app.hscphone:id/bg_login').click()
        time.sleep(1)
        # 设置账号和密码
        phone = driver.find_element_by_id('com.app.hscphone:id/et_phone')
        phone.send_keys('13713581347')
        time.sleep(1)
        pwd = driver.find_element_by_id('com.app.hscphone:id/et_password')
        pwd.send_keys('123456')
        time.sleep(1)
        driver.find_element_by_id('com.app.hscphone:id/tv_login').click()

    pass


# 下拉刷新
def swipeDown(driver, n=5):
    time.sleep(2)
    '''定义向下滑动方法'''
    # // 获取控件开始位置的坐标轴
    size = driver.find_element_by_id('com.app.hscphone:id/iv_head').size
    location = driver.find_element_by_id('com.app.hscphone:id/iv_head').location
    print('swipeDown---size ', size)
    print('swipeDown--location ', location)
    startX = endX = location.get('x') + size.get('width') / 2
    startY = location.get('y') + 10
    endY = location.get('y') + size.get('height') / 2
    # 下拉刷新
    driver.swipe(startX, startY, endX, endY, duration=2000)


if __name__ == '__main__':
    test()
